layui.use(['table', 'laytpl', 'layer', 'laydate'], function() {
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laydate = layui.laydate;
	var laytpl = layui.laytpl;
	var tablelist;
	getannounce();
	var postTime;
	$("#searchBtn").on("click", function() {
		postTime = $("#postTime").val();
		getannounce();
	})
	$("#reLoad").on("click", function() {
		postTime = "";
		layer.msg("刷新",{time:500}, function() {
			getannounce();
		});
	})
	$("#delBtn").on("click", function() {
		var checkStatus = table.checkStatus('table_info');
		if(checkStatus.data.length == 0) {
			layer.alert("请至少选择一条进行删除");
			return;
		}
		var data = checkStatus.data;
		var id = new Array();
		for(var i = 0; i < data.length; i++) {
			id.push(data[i].id);
		}
		deleteMore(id);
	})

	function getannounce() {
		getannou();

		function getannou() {
			$.ajax({
				type: "post",
				url: "/getannounce",
				data: {
					postTime: postTime
				},
				async: true,
				success: function(data) {
					var result = $.parseJSON(data);
					tablelist = table.render({
						elem: '#test',
						height: 'full-95',
						page: true, //开启分页,
						limits: [10],
						limit: 10,
						id: 'table_info',
						cols: [
							[ //表头
								{
									checkbox: true,
									fixed: true
								},
								{
									field: 'announcename',
									title: '公告标题',
									width: 150,
									sort: true,
								}, {
									field: 'announcecontxt',
									title: '公告内容',
									width: 150
								}, {
									field: 'announcetime',
									title: '公告发布时间',
									width: 200,
									sort: true
								}, {
									field: 'announcedate',
									title: '公告发布日期',
									width: 180
								}, {
									field: 'announceman',
									title: '发布人',
									width: 170
								}, {
									fixed: 'right',
									title: '操作',
									width: 300,
									align: 'center',
									toolbar: '#barDemo'
								}
							]
						],
						"data": result
					});
				}
			});
		}

	}
	table.on('tool(lay_table_filter)', function(obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
		var data = obj.data //获得当前行数据,
		layEvent = obj.event; //获得 lay-event 对应的值
		if(layEvent === 'del') {
			deleteMore(data.id);
		} else if(layEvent === 'edit') {
			detail(data.id, "edit");
		} else if(layEvent === 'detail') {
			checkDetails(data.id);
		}
	});
	$("#addBtn").on("click", function() {
		detail(" ", "add");
	});
	//表格中的查看事件
	function checkDetails(id) {
		var iframeId = new Date().getTime().toString();
		layer.open({
			id: iframeId,
			title: '公告详情',
			type: 2,
			content: '/static/msg/announce/announce_detail.html?id=' + id,
			area: ['360px', '400px'],
			anim: -1, //关闭动画 兼容ie6-9需设置此项
			isOutAnim: false, //关闭动画  兼容ie6-9需设置此项
			shadeClose: false,
			btn: ['确认', '取消'],
			yes: function(index, layero) {
				layer.close(index);
			}
		});
	}
	//表格中的编辑事件
	function detail(id, type) {
		var iframeId = new Date().getTime().toString();
		layer.open({
			id: iframeId,
			title: '编辑公告',
			type: 2,
			content: '/static/msg/announce/announce_edit.html?id=' + id + "&type=" + type,
			area: ['360px', '400px'],
			anim: -1, //关闭动画 兼容ie6-9需设置此项
			isOutAnim: false, //关闭动画  兼容ie6-9需设置此项
			shadeClose: false,
			btn: ['确认', '取消'],
			yes: function(index, layero) {
				var body = layer.getChildFrame('body', index);
				var announcetitle = body.find(".announcetitle").val();
				var announcecontxt = body.find(".layui-textarea").val();
				var date = new Date();
				var year = date.getFullYear();
				var month = date.getMonth() + 1;
				var day = date.getDate();
				var hour = date.getHours();
				var minute = date.getMinutes();
				var second = date.getSeconds();
				var riqi = year + "-" + month + "-" + day;
				var time = hour + ":" + minute + ":" + second;
				var isgo=true;
				if(announcetitle==""){
					layer.msg('公告标题不能为空',{time:2000});
					isgo=false;
				}
				if(isgo){
					if(announcecontxt==""){
						layer.msg('公告内容不能为空',{time:2000});
						isgo=false;
					}
				}
				
				if(isgo) {
					$.ajax({
						type: "post",
						url: "/announce/edit",
						data: {
							id: id,
							announcename: announcetitle,
							announcecontxt: announcecontxt,
							type: type,
							riqi: riqi,
							time: time
						},
						async: true,
						success: function(data) {
							layer.msg(data.massage,{time:1000}, function() {
								if(data.code == '1') {
									layer.close(index);
									getannounce();
								}
							});
						}
					});
				} 
			}
		});
	}

	//表格中的删除事件
	function deleteMore(id) {
		if("" == id) {
			layer.alert("请至少选择一条信息!");
			return;
		}
		layer.confirm('确认删除此信息吗？', {
			icon: 3,
			title: '提示'
		}, function(index) {
			$.ajax({
				type: "post",
				url: "/announce/delect",
				async: true,
				data: {
					id: id
				},
				success: function(data) {
					layer.msg(data.message,{time:2000});
					getannounce();
				}
			});
			layer.close(index);
		});
	}
	laydate.render({
		elem: '#postTime' //指定元素
	});
});